﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Internal = AbstraX.BuildEvents;
using Microsoft.Build.Framework;
using System.Collections;

namespace CodeGenerationPipeline
{
    public static class EventExtension
    {
        public static void Read(this Internal.BuildErrorEventArgs eInternal, BuildErrorEventArgs e)
        {
            eInternal.Code = e.Code;
            eInternal.ColumnNumber = e.ColumnNumber;
            eInternal.EndColumnNumber = e.EndColumnNumber;
            eInternal.EndLineNumber = e.EndLineNumber;
            eInternal.File = e.File;
            eInternal.LineNumber  = e.LineNumber;
            eInternal.ProjectFile = e.ProjectFile;
            eInternal.Subcategory = e.Subcategory;

            eInternal.Read((BuildEventArgs)e);
        }

        public static void Read(this Internal.BuildEventArgs eInternal, BuildEventArgs e)
        {
            var context = new Internal.BuildEventContext();

            if (e.BuildEventContext != null)
            {
                context.Read(e.BuildEventContext);
            }

            eInternal.BuildEventContext = context;
            eInternal.HelpKeyword = e.HelpKeyword;
            eInternal.Message = e.Message;
            eInternal.SenderName = e.SenderName;
            eInternal.ThreadId = e.ThreadId;
            eInternal.Timestamp = e.Timestamp;
        }

        public static void Read(this Internal.BuildEventContext contextInternal, BuildEventContext context)
        {
            contextInternal.BuildRequestId = context.BuildRequestId;
            contextInternal.NodeId = context.NodeId;
            contextInternal.ProjectContextId = context.ProjectContextId;
            contextInternal.ProjectInstanceId = context.ProjectInstanceId;
            contextInternal.SubmissionId = context.SubmissionId;
            contextInternal.TargetId = context.TargetId;
            contextInternal.TaskId = context.TaskId;
        }

        public static void Read(this Internal.BuildFinishedEventArgs eInternal, BuildFinishedEventArgs e)
        {
            eInternal.Succeeded = e.Succeeded;

            eInternal.Read((BuildStatusEventArgs)e);
        }

        public static void Read(this Internal.BuildMessageEventArgs eInternal, BuildMessageEventArgs e)
        {
            eInternal.Importance = (Internal.MessageImportance)e.Importance;

            eInternal.Read((BuildEventArgs)e);
        }

        public static void Read(this Internal.BuildStartedEventArgs eInternal, BuildStartedEventArgs e)
        {
            eInternal.BuildEnvironment = new Dictionary<string, string>();

            if (e.BuildEnvironment != null)
            {
                e.BuildEnvironment.ToList().ForEach(k => eInternal.BuildEnvironment.Add(k.Key, k.Value));
            }

            eInternal.Read((BuildStatusEventArgs)e);
        }

        public static void Read(this Internal.BuildStatusEventArgs eInternal, BuildStatusEventArgs e)
        {
            eInternal.Read((BuildEventArgs)e);
        }

        public static void Read(this Internal.BuildWarningEventArgs eInternal, BuildWarningEventArgs e)
        {
            eInternal.Code = e.Code;
            eInternal.ColumnNumber = e.ColumnNumber;
            eInternal.EndColumnNumber = e.EndColumnNumber;
            eInternal.EndLineNumber = e.EndLineNumber;
            eInternal.File = e.File;
            eInternal.LineNumber = e.LineNumber;
            eInternal.ProjectFile = e.ProjectFile;
            eInternal.Subcategory = e.Subcategory;

            eInternal.Read((BuildEventArgs)e);
        }

        public static void Read(this Internal.CustomBuildEventArgs eInternal, CustomBuildEventArgs e)
        {
            eInternal.Read((BuildEventArgs)e);
        }

        public static void Read(this Internal.ProjectFinishedEventArgs eInternal, ProjectFinishedEventArgs e)
        {
            eInternal.ProjectFile = e.ProjectFile;
            eInternal.Succeeded = e.Succeeded;

            eInternal.Read((BuildStatusEventArgs)e);
        }

        public static void Read(this Internal.ProjectStartedEventArgs eInternal, ProjectStartedEventArgs e)
        {
            var context = new Internal.BuildEventContext();

            context.Read(e.ParentProjectBuildEventContext);

            eInternal.Items = new List<DictionaryEntry>();
            eInternal.Properties = new List<DictionaryEntry>();

            if (e.Items != null)
            {
                e.Items.Cast<DictionaryEntry>().ToList().ForEach(d => eInternal.Items.Add(d));
            }

            if (e.Properties != null)
            {
                e.Properties.Cast<DictionaryEntry>().ToList().ForEach(d => eInternal.Properties.Add(d));
            }

            eInternal.ParentProjectBuildEventContext = context;
            eInternal.ProjectFile = e.ProjectFile;
            eInternal.ProjectId = e.ProjectId;
            eInternal.TargetNames = e.TargetNames;

            eInternal.Read((BuildStatusEventArgs)e);
        }

        public static void Read(this Internal.TargetFinishedEventArgs eInternal, TargetFinishedEventArgs e)
        {
            eInternal.TargetOutputs = new List<string>();

            if (e.TargetOutputs != null)
            {
                e.TargetOutputs.Cast<string>().ToList().ForEach(s => eInternal.TargetOutputs.Add(s));
            }

            eInternal.ProjectFile = e.ProjectFile;
            eInternal.Succeeded = e.Succeeded;
            eInternal.TargetFile = e.TargetFile;
            eInternal.TargetName = e.TargetName;

            eInternal.Read((BuildStatusEventArgs)e);
        }

        public static void Read(this Internal.TargetStartedEventArgs eInternal, TargetStartedEventArgs e)
        {
            eInternal.ProjectFile = e.ProjectFile;
            eInternal.TargetFile = e.TargetFile;
            eInternal.TargetName = e.TargetName;
            eInternal.ParentTarget = e.ParentTarget;

            eInternal.Read((BuildStatusEventArgs)e);
        }

        public static void Read(this Internal.TaskFinishedEventArgs eInternal, TaskFinishedEventArgs e)
        {
            eInternal.ProjectFile = e.ProjectFile;
            eInternal.Succeeded = e.Succeeded;
            eInternal.TaskFile = e.TaskFile;
            eInternal.TaskName = e.TaskName;

            eInternal.Read((BuildStatusEventArgs)e);
        }

        public static void Read(this Internal.TaskStartedEventArgs eInternal, TaskStartedEventArgs e)
        {
            eInternal.ProjectFile = e.ProjectFile;
            eInternal.TaskFile = e.TaskFile;
            eInternal.TaskName = e.TaskName;

            eInternal.Read((BuildStatusEventArgs)e);
        }
    }
}
